package com.xm9m.web.service.api;

import com.salesmanager.core.modules.sms.SmsService;
import com.salesmanager.core.business.customer.model.Customer;
import com.salesmanager.core.business.customer.service.CustomerService;
import com.salesmanager.web.entity.SMSMessage;
import com.salesmanager.web.entity.SMSType;
import com.salesmanager.web.utils.SMS.ValidateCode;
import com.xm9m.web.service.api.request.PasswordRequest;
import com.xm9m.web.service.constants.StatusCode;
import com.xm9m.web.service.interfaces.AbstractRestApi;
import com.xm9m.web.service.interfaces.IXm9mResponse;
import com.xm9m.web.service.internal.util.BuildResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Map;


public class ChangePasswordUpdate extends AbstractRestApi {

    private static final Logger logger = LoggerFactory.getLogger(ChangePasswordUpdate.class);

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SmsService smsService;

    @SuppressWarnings("unchecked")
    @Override
    public <T extends IXm9mResponse> T execute(String requestData, String version) {
        Map<String, Object> map;
        PasswordRequest passwordRequest = super.json2Bean(requestData, PasswordRequest.class);
        if (passwordRequest == null) {
            return  (T) BuildResponseUtil.buildSimpleResponse(StatusCode.PARAM_WRONG);
        }
        //使用spring security存储的电话来验证
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if ((auth instanceof AnonymousAuthenticationToken)) {
            return  (T) BuildResponseUtil.buildSimpleResponse(StatusCode.USER_NO_EXISTS);
        }
        Customer customer = customerService.getByTelphone(auth.getName());
        if (customer == null) {
            return  (T) BuildResponseUtil.buildSimpleResponse(StatusCode.USER_NO_EXISTS);
        }
        try {
            SMSMessage originMessage =(SMSMessage) smsService.getByTelephone(passwordRequest.getMobile());
            SMSMessage newMessage=new SMSMessage();
            newMessage.setCode(passwordRequest.getCaptcha());
            newMessage.setPhone(passwordRequest.getMobile());
            newMessage.setType(SMSType.CUSTOMER_PASSWORD_CHANGE.getValue());
            map = ValidateCode.validate(originMessage, newMessage);

            String valid = (String) map.get("valid");
            if ("false".equals(valid)) {
                return  (T) BuildResponseUtil.buildSimpleResponse(StatusCode.CAPTCHA_ERROR);
            }

            boolean b = customerService.updatePassword(passwordRequest.getMobile(), passwordRequest.getNewPwd());
            if (!b) {
                return  (T) BuildResponseUtil.buildSimpleResponse(StatusCode.PARAM_WRONG);
            }
        } catch (Exception ee) {
            return  (T) BuildResponseUtil.buildSimpleResponse(StatusCode.UNKNOWN_ERROR);
        }
        //删除短信
        smsService.delete(passwordRequest.getMobile());
        return  (T) BuildResponseUtil.buildSimpleResponse(StatusCode.SUCCESS);


    }
}